

<script src="history.js"></script>
<script src="projections.js"></script>

<script>


/**********************************************

TODO

scale verticle data and y axis labels

change actual line to solid and future to dashed.
add dot to future...

put this in a template and render it

******************************************************/







    var ONE_WEEK_IN_SECONDS = 60 *60 * 24 * 7;
    
    
    var history_array = new Array();
    var proj_array = new Array();
    var current_array = new Array();
    
    
  //  var projection_data = [[1181883600,'20'],[1182488400,'24'],[1183093200,'20'],[1183698000,'15'],[1184302800,'3']]; 
                         
  //  var history_data = [[1181883600,'30'],[1182488400,'34'],[1183093100,'25']]; //,[1183698000,'22'],[1184302800,'16']];                
                            
/*
1184302800,25
1184907600,25
1185512400,25
1186117200,28
1186722000,31
1187326800,34
1187931600,30
1188536400,28
1189141200,25
1189746000,21
1190350800,18
1190955600,16
1191560400,16
1192165200,16
1192770000,16
1193374800,16
*/

    //var history_data = new Array('2','5','9','33','-1','-1');
   // var projection_data = new Array('15','30','33','33','5','6','8');
    var current_count = new Array('-1','-1','-1','33', '15','-1');
    var chart_props = new Array(
    'http://chart.apis.google.com/chart?chs=320x200',
    'cht=lxy',
    'chs=700x375',
    'chtt=Test Graph',
    'chds=0,60','0','60','0','10',
    'chls=2,6,3|2,1,0|2,1,0',
    'chco=0000FF,FF6600,000000',
    'chdl=Actual|Projections|Current',
    'chm=N,000000,0,-1,12,1|s,0000FF,0,-1,7,1|o,FF6600,1,-1,8,1',             
    'chdlp=b',
    'chxt=x,y',
    'chxl=0:' + calulate_x_axis_label() + '1:|0|10|20|30|40|' );

 //'chxl=0:|jan|feb|mar|apr|may|jun',
 //'chxl=0:' + calulate_x_axis_label(),
    function get_url()
    {
      return chart_props.join("&") + "&" + chart_data();
    }
    
    function chart_data()
    {
        return 'chd=t:-1|' + history_array.join(',') + '|-1|' + proj_array.join(',')  + '|-1|' + current_array.join(',');
    }

    function render_url()
    {
        return '<img src="' + get_url() + '">';
    }

    function get_max_value()
    {
    
    }
    
    
    function calculate_projection_data()
    {
        
        
        for  (var j = 0; j < projection_data.length; j++) 
        {
            proj_array.push( projection_data[j][1]);
        } 
        return proj_array.join(',');
    }
    
    
    // for every date in the projection data find a matching history entry
    // if there is no entry, put -1 so nothing is drawn on the graph
    function calculate_history_data()
    {
        
        for  (var j = 0; j < projection_data.length; j++) 
        {
            // you might have history after the current date,
            // but we don't want to show that on the history line
            // because it's not fixed yet. We'll handled the current count
            // on the current count line.
            if ( projection_data[j][0] <= current_date )
            {
                var history_for_date = get_history_value_for_date(projection_data[j][0], history_data);
                if ( history_for_date != -1 )
                {
                    // this is the last projection index for which there is an 
                    // history entry
                    last_projection_index_with_absolute_history = j;
                }
                history_array.push(history_for_date);
            }
            else
            {
                history_array.push(-1);
            }
            
        }  
        
    }
    
    
  
    
    
    function get_history_value_for_date(requested_date, history_data)
    {
        var the_answer = '-1';
        //document.write("request date: " + requested_date + "<br>");
        for  (var j = 0; j < history_data.length; j++) 
        {
           if ( history_data[j][0] <= requested_date && 
                 history_data[j][0] > (requested_date - ONE_WEEK_IN_SECONDS )   )
           {
           
                // the count found is the bug count on or before 
                the_answer = history_data[j][1];
                // don't do any more checks for this date
                
                
           }  
        } 
        return the_answer;
     } 
     


     
     function calculate_current_data()
     {
        
        
        for  (var j = 0; j < projection_data.length; j++) 
        {   
            if ( j < last_projection_index_with_absolute_history )
            {
                // don't show anything for the day's we have history
                current_array.push(-1);
            }
            else if ( j == last_projection_index_with_absolute_history )
            {
                current_array.push(history_array[j]);
            }
            else if ( j - 1 == last_projection_index_with_absolute_history )
            {
                current_array.push(get_history_value_for_date(projection_data[j][0], history_data));
            }
            else
            {
                current_array.push(-1);
            }
        
        
        }
        
        
     }
        
     function calulate_x_axis_label()
     {
        //it has to look like this 
        //'chxl=0:|jan|feb|mar|apr|may|jun'
        var chxl = '';
        for  (var j = 0; j < projection_data.length; j++) 
        {
            var d = new Date( projection_data[j][0] *1000 );
            var curr_date = d.getDate();
            var curr_month = d.getMonth();
            curr_month++;
            chxl += '|' + curr_month + '/' + curr_date;
        }
        
        return chxl + '|'; 
        
     }
     calculate_projection_data();
     calculate_history_data();
     calculate_current_data();
     
     

    
</script>



<script>
document.writeln("projections: " + proj_array.join(','));</script>
<br>
<script>document.writeln("history: " + history_array.join(','));</script>
<br>
<script>document.writeln("current: " + current_array.join(','));</script>
<br>
<script>document.writeln("x axis: " + calulate_x_axis_label());</script>
<script>document.writeln(render_url());
</script>

